package com.example.fastshare;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import com.example.fastshare.ListItemAdapter;
import com.example.fastshare.adapter.DBMesssageAdapter;
import com.example.fastshare.adapter.DBUserAdapter;
import com.example.fastshare.alljoyn.MainApp;
import com.example.fastshare.entities.MessageEntity;
import com.example.fastshare.entities.UserEntity;

public class HistoryFragment extends Fragment {

	public static ArrayList<Item> _list = null;
	public static ListItemAdapter _listAdapter = null;
	private MainApp m_mainApp;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_list = new ArrayList<Item>();
		LoadHistory();

		_listAdapter = new ListItemAdapter(this.getActivity(),
				android.R.layout.activity_list_item, _list);

		View rootView = inflater.inflate(R.layout.history, container, false);
		ListView _listView = (ListView) rootView
				.findViewById(R.id.listConversation);

		_listView.setAdapter(_listAdapter);
		_listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Item item = _listAdapter.getItem(arg2);
				Intent myIntent = new Intent(getActivity(),
						ConversationActivity.class);
				myIntent.putExtra("UserInfo", item.getUserId());
				getActivity().startActivity(myIntent);
				return true;
			}
		});
		// super.onCreateView(inflater, container, savedInstanceState);
		m_mainApp = (MainApp) getActivity().getApplication();
		m_mainApp.chechIn();
		return rootView;
	}

	public Boolean LoadHistory() {
		DBUserAdapter userAdapter = new DBUserAdapter(this.getActivity());
		DBMesssageAdapter messageAdapter = new DBMesssageAdapter(
				this.getActivity());
		List<UserEntity> userList = userAdapter.getAllUser();
		if (userList == null) {
			return false;
		}

		for (UserEntity user : userList) {
			MessageEntity result = messageAdapter.getLastMessagesOfUser(user
					.getID());
			if (result != null) {
				if (result.getContent().equals("")) {
					Item item = new Item(user.getAvatar_Path(), "Image!!!",
							null, false);
					item.setStrTime(result.getTimeInString());
					item.setUserId(user.getID());
					_list.add(item);

				} else {

					Item item = new Item(user.getAvatar_Path(),
							result.getContent(), null, false);
					item.setStrTime(result.getTimeInString());
					item.setUserId(user.getID());
					_list.add(item);

				}

			}

		}
		return true;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onResume()
	 */
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		_list = new ArrayList<Item>();
		LoadHistory();
	}
}
